﻿<extend name="Public/content" />
<block name="style">
<style type="text/css">
    .table>tbody>tr>td{ padding: 4px; vertical-align: middle;}
        body{ background: #fff;}
   .footer {
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 0;
   
    left: 0;
    line-height: 50px;
    text-align: right;
    text-align: right;
    border-top: solid 1px #eee;
    padding-right: 10px;
    z-index: 999999999999999;
}
</style>
</block>
   <block name="body">
    <div id="main_list">
        <input type="hidden" name="delivery_id" value="{$delivery_id}" id="delivery_id">
    <include file="Public:artdialog" />
    <div class="row">
        <div class="col-md-12">
            <div class="block-web">
                <div class="header">
                    <h3 class="content-header">我的购物车</if></h3>
                </div>

                <div class="porlets-content">

                    <div class="table-responsive">
                        <div id="order_list_item_content">
                                <table id="sample-table-2" class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>商品名称</th>
                                            <th>商品编号</th>
                                            <th>价格</th>
                                            <th>数量</th>
                                            <th>商品合计</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="lists_order">
                         
                                    </tbody>
                                </table>
                                <div>
                                    <p class="alert alert-success right" style="text-align:right" id="sum_htm">
                                                       
                                    </p>
                                </div>
                        </div>
                    </div><!--/table-responsive-->
                </div><!--/porlets-content-->
            </div><!--/block-web-->
        </div><!--/col-md-12-->
    </div>

      <div class="header">
                    <h3 class="content-header">填写收件人信息</if></h3>
      </div>
      <div class="form-horizontal">
<div class="form-group ">
        <label class="col-sm-3 control-label no-padding-left" for="form-field-1"> 收件人姓名 </label>

        <div class="col-sm-5">
            <input type="text" id="name" onblur="check_old_pwd(this,0)" placeholder="收件人姓名" class="col-xs-10 col-sm-5">
             <span  style="display:none"> <i class="icon-ok green" ></i></span>
        </div>
    </div>

    <div class="form-group ">
        <label class="col-sm-3 control-label no-padding-left" for="form-field-1"> 收件人联系方式 </label>

        <div class="col-sm-5">
            <input type="text" id="mobile" onblur="check_old_pwd(this,1)" placeholder="收件人联系方式" class="col-xs-10 col-sm-5">
             <span  style="display:none"> <i class="icon-ok green" ></i></span>
        </div>
    </div>
        <div class="form-group ">
        <label class="col-sm-3 control-label no-padding-left" for="form-field-1"> 收件人地址 </label>

        <div class="col-sm-9">
            <input type="text" id="address" onblur="check_old_pwd(this,2)" placeholder="收件人地址" class="col-xs-10 col-sm-5">
             <span  style="display:none"> <i class="icon-ok green" ></i></span>
        </div>
    </div>
            <div class="form-group ">
        <label class="col-sm-3 control-label no-padding-left" for="form-field-1"> 身份证信息 </label>

       
            <button type="button" id="idcard_buttom" class="btn btn-sm btn-success"   onclick="_es_openDilog90('{:U('Order/upload_idcard',array('source_order_sn'=>$source_order_sn,'type'=>9))}','上传身份证','shangchuanshenfenzheng')"> 上传身份证</button>
          <span style="color:red;font-weight:bold" id="idcard_msg">(*没有上传身份证)</span>
          <input type="hidden" id="idcard" value="" >
          <input type="hidden" id="IdcardSysKey" value="" >
    </div>
</div>
    <div class="footer" >
        <button class="btn btn-info " type="button" onclick="sub_order_list_all()" >提交下单</button>
        <button class="btn btn-default bt_close" type="button" onclick="del_order_list_all()" >取消关闭</button>
    </div>
    <div style="height:50px"></div>
    </div>
</block>

<block name="script">

<script type="text/javascript">
var source_order_sn = "{$source_order_sn}";
 var cookiename2="goods_order_<?php echo UID; ?>";
 var is_mobile=0;
 var is_name=0;
 var is_address=0;
    $(function(){
        //初始化
        load_goods();
        //修改数量，重新计算价格
       
        $("input[type=number]").on('input',function(e){
           var old_number =  $(this).attr('number');
           var json_number =$(this).attr('json_number');
           var salenumber =  $(this).attr('salenumber');
           var quantity =  $(this).attr('quantity');
           var goods_number = $(this).val();
           var goods_price = $(this).parent('td').prev().html();
           if(goods_number >(quantity-salenumber)){
              $(this).val(quantity-salenumber);
              showvfmsg(0, '商品数量大于可售数量', 3000);return;
           }
           $(this).parent('td').next().html((goods_number*goods_price).toFixed(2));
           var sum_price_a = $('#sum_price_float').html();
           var sum_price_new = parseFloat(sum_price_a -(old_number*goods_price) + (goods_number*goods_price) );
           $('#sum_price_float').html(sum_price_new.toFixed(2));
           $(this).attr('number',goods_number);
        }); 
    });

function check_old_pwd(_this,type){
     var _list = $.trim($(_this).val());
     var res= true;
     if(_list == ""){
         res = false;
      }
    if(type==0){
      //名字
      if(_list.length>20){
        res =false;
      }else{
        is_name =1;
      }
    }else if(type==1){

      //电话
      if(!check_mobile(_list)){
         res =false;
      }else{
        is_mobile=1;
      }
    }else if(type==2){
      //地址
       if(_list != ''){
        is_address =1;
       }
    }

  if(!res){
         $(_this).parent().children('span').show();
         $(_this).parent().children('span').html('<i class="icon-remove red"></i>');
         return false;
       }else{
        idcard_http = 1;
        $(_this).parent().children('span').show();
        $(_this).parent().children('span').html('<i class="icon-ok green"></i>');
      }


    }

  function check_mobile(mobile){
   var _mobile=/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/;   
        return _mobile.test(mobile); 
   
  }  


 function load_goods(){
   var ojson=[],lsjson=[],json=[],del_json=[]; 
   var htl="";
    //获取cookie值 
    var _list=getCookie(cookiename2);
    if(_list!=""){
      ojson=JSON.parse(_list);
    }
    var htm = "";
    if(ojson == null){
        _hide_public_loading();
        return;
    }
   
    var sum_price = 0;
    var num = 0;
    for(var i=0;i<ojson.length;i++){
        sum_price += parseFloat((ojson[i].goods_price*ojson[i].number).toFixed(2)) ;
        num +=1;
        htm +='<tr><td>'+ojson[i].goods_name+'</td><td>'+ojson[i].goods_sn+'</td><td>'+ojson[i].goods_price+'</td><td><input  style="width:80px"  type="number" min="1" json_number='+ojson[i].number+' number='+ojson[i].number+' salenumber='+ojson[i].salenumber+' quantity='+ojson[i].quantity+'  value='+ojson[i].number+'></td><td>'+(ojson[i].goods_price*ojson[i].number).toFixed(2)+'</td><td><button type="button" class="btn btn-info btn-sm" field=\'"goods_id":"'+ojson[i].goods_id+'","goods_sn":"'+ojson[i].goods_sn+'","goods_price":"'+ojson[i].goods_price+'","salenumber":"'+ojson[i].salenumber+'","quantity":"'+ojson[i].quantity+'","number":"'+ojson[i].number+'"\' onclick="del_order_list(this)"> 删除</button></td></tr>';
    }
    var htm2 = '一共有<span style="color:red;font-weight:bold;" id="sum_num_int">'+num+'</span>件商品,商品总价<span style="color:red;font-weight:bold;" id="sum_price_float">'+sum_price.toFixed(2)+'</span> AU$';
    $('#lists_order').append(htm);
    $('#sum_htm').append(htm2);
    _hide_public_loading();
 }


//身份证弹窗
function _es_openDilog90(url, title,id){ 
     art.dialog.open(url, { title: title, width: '800px', height: '70%', opacity: 0.2, id: id, fixed: true,lock:true, close:function(){
      console.log($('#idcard_message').val());
      var idcard = $('#idcard').val();
      var IdcardSysKey =$('#IdcardSysKey').val();
      if(idcard !="" && IdcardSysKey !="" ){
               $('#idcard_msg').html('(身份证上传成功)').css('color','#87b87f');
               $('#idcard_buttom').attr({"disabled":"disabled"});
      }

     }  });
}
 //删除cookie中的数据
 function del_order_list(_this){
    var dia=art.dialog({
    title: '提示',
    content: '您确定要从购物车中移除该商品吗？',
    // lock:true,
    cancel:true,
    ok:function(){
      //获取到要删除的商品的数量
      var del_nubmer = $(_this).parent('td').prev().prev().find('input').val();
     var ojson=[],lsjson=[],json=[],del_json=[];
     var _json = $(_this).attr('field');
     _json="{"+_json+"}";
     var tmp =JSON.parse(_json);
    //删除html
    $(_this).parent('td').parent('tr').remove();
     //获取cookie
    var _list=getCookie(cookiename2);
    if(_list!=""){
      ojson=JSON.parse(_list);
    }
    for(var i=0;i<ojson.length;i++){
        if(ojson[i].goods_id == tmp['goods_id']){
            var del_price_num = parseFloat(ojson[i].goods_price*del_nubmer);
            del_json.push(tmp);  //删除的
        }else{
            lsjson.push(ojson[i]); //剩余的
        }
    }

    if(lsjson.length>0){
        setCookie(cookiename2,"",-10);
        $('#sum_num_int').html(lsjson.length);
        var old_sum_price = $('#sum_price_float').html();
        $('#sum_price_float').html((old_sum_price-del_price_num).toFixed(2));
        var _db=JSON.stringify(lsjson);
        setCookie(cookiename2,_db,1);
       
         showvfmsg(1, '删除成功', 3000);return;
    }else{
        if(del_json.length>0){
            $('#sum_num_int').html(0);
            $('#sum_price_float').html(0);
            setCookie(cookiename2,"",-10);
            showvfmsg(1, '删除成功', 3000);return;
        }
    }
    },
    cancelVal: '取消',
    cancel:true,

  });

 }

 //下单
 function sub_order_list_all(){
    var lsjson =[];

   //获取到采购单内容
      var GoodsJsonStrObj = [];
   $("#lists_order").children('tr').each(function(){
        var new_number = $(this).children('td').eq(3).find('input').val();
        var _json=$(this).children('td').eq(5).find('button').attr("field");
        var _json="{"+_json+"}";
        var tmp =JSON.parse(_json);
        //拼接下单商品格式
         var arr = {};
         arr.Quantity = new_number;
         arr.GoodsSn = tmp.goods_sn;
         if(arr != ""){
            GoodsJsonStrObj.push(arr);
         }
        if(tmp != ""){
           lsjson.push(tmp);  //当前特价商品信息
        }
   });
   if(GoodsJsonStrObj.length <=0){
    showvfmsg(0, '请先选择商品', 3000);return;
   }

     if(is_name==0){
    showvfmsg(0, '请填写正确的收货人', 3000);return;
  }
  if(is_mobile==0){
    showvfmsg(0, '请填写正确的联系方式', 3000);return;
  }
  if(is_address==0){
    showvfmsg(0, '请填写正确的收货地址', 3000);return;
  }
   // console.log(GoodsJsonStrObj);
    var datas = {};
    datas.GoodsJsonStr =JSON.stringify(GoodsJsonStrObj);
    //获取收货人信息
     datas.source_order_sn =source_order_sn;
     datas.consignee = $('#name').val();
     datas.mobile = $('#mobile').val();
     datas.address = $('#address').val();
     var idcard = $('#idcard').val();
     var IdcardSysKey = $('#IdcardSysKey').val();
     if(idcard !=""){
         datas.idcard = idcard;
     }
     if(IdcardSysKey != ""){
        datas.IdcardSysKey = IdcardSysKey;
     }
     // console.log(datas);
    _show_public_loading();
   //下单
         $.ajax({
             'url': "{:U('Order/Warehouse_order')}",
             'data': datas,
              // 'dataType': 'html',
             'type': 'POST',
             success: function (data) {

                // $('#main_list').html(data);
                //关掉购物车窗口，弹出下单信息

            // art.dialog.open.api.close();
                  if(data.Message ==null){
                    data.Message = "下单失败!";
                  }
                  showvfmsg(data.state, data.Message, 3000);
                 if (data.state =="1") {  
                    setTimeout(function(){ 
                      del_order_list_all();
                    },3000);
                 }
                 // location.href = "{:U('Order/order_msg_prompt')}";
             }, error: function (err) { 
                 console.log(err);
             },complete: function () {
                  _hide_public_loading();
             }
         });

 }
 //删除cookie中的数据
 function del_order_list_all(){
     setCookie(cookiename2,"",-10);
     art.dialog.open.api.close();
 }

//写入cookie
 function setCookie(c_name, value, expiredays){
    if(expiredays < 0){
        localStorage.removeItem(c_name);
    }
       localStorage.setItem(c_name,value); //存储名字为name值为caibin的变量
 }
//读取cookie   
 function getCookie(c_name){
    return  localStorage.getItem(c_name);
 }

function clock_load_dialog(){
   setTimeout(function(){
    var win=    art.dialog.opener;
                win.load();
                top.art.dialog().close();
    },800);
}




</script>
</block>
